package com.wen.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.Result;
import com.wen.entity.*;
import com.wen.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 会员管理
 *
 * @author dhh
 * @date 2021/11/17 11:38
 */
@RestController
@AllArgsConstructor
@RequestMapping("/member")
public class MemberController {

    private final MemberService memberService;

    private final UserService userService;

    private final MemberOrderService memberOrderService;

    private final EquipmentService equipmentService;

    private final ConsumRecordService consumRecordService;

    //    添加设备信息
    @PostMapping("/insert")
    public Result<?> save(@RequestBody Member member) {

        User user = new User();
        user.setUsername(member.getUserName());
        user.setPassword("123456");
        user.setAge(member.getAge());
        user.setPhone(member.getPhone());
        user.setSex(member.getSex());
        user.setRole(2);
        userService.save(user);
        if (member.getBalance()==null){
            member.setBalance(0.00);
            member.setAllMoney(0.00);
        }
        if (member.getAllMoney() != null) {
            member.setBalance(member.getAllMoney() + member.getBalance());
        }
        member.setUserId(user.getId());
        memberService.save(member);
        return Result.success();
    }

    //    s删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        memberService.removeById(id);
        return Result.success();
    }

    //    更新
    @PutMapping("/updateById")
    public Result<?> updateById(@RequestBody Member member) {
        if (member.getAllMoney() != null) {
            member.setBalance(member.getAllMoney() + member.getBalance());
        }
        memberService.updateById(member);
        User user = new User();
        user.setId(member.getUserId());
        user.setAge(member.getAge());
        user.setPhone(member.getPhone());
        user.setSex(member.getSex());
        userService.updateById(user);
        return Result.success();
    }

    //    查询会员列表
    @GetMapping("/findPage")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<Member> NewsPage = memberService.page(new Page<>(pageNum, pageSize), null);
        return Result.success(NewsPage);
    }

    //    查询会员列表
    @GetMapping("/findMemberId")
    public Result<?> findMemberId(@RequestParam Integer id) {
        LambdaQueryWrapper<Member> wrapper = Wrappers.<Member>lambdaQuery();
        if (id != null) {
            wrapper.like(Member::getId, id);
        }
        List<Member> mapList = memberService.list(wrapper);
        return Result.success(mapList);
    }


    //    查询我的课程
    @GetMapping("findCourse/{memberId}")
    public Result<?> findCourse(@PathVariable String memberId) {
        if (StrUtil.isBlank(memberId)) {
            return Result.error("-1", "会员ID不能为空");
        }
        LambdaQueryWrapper<ConsumRecord> wrapper = Wrappers.<ConsumRecord>lambdaQuery();
        wrapper.eq(ConsumRecord::getMemberId, memberId);
        return Result.success(consumRecordService.list(wrapper));
    }


    @GetMapping("/findMember")
    public Result<?> findMember(@RequestParam Integer coachId, @RequestParam String search) {
        List<Member> mapList = memberService.findMember(coachId, search);
        return Result.success(mapList);
    }

    //    选课程和教练
    @PostMapping("/save")
    public Result<?> save(@RequestBody ConsumRecord consumRecord) {
        consumRecordService.save(consumRecord);
        return Result.success();
    }

    //    更新课程和教练
    @PutMapping("/update")
    public Result<?> update(@RequestBody ConsumRecord consumRecord) {
        consumRecordService.updateById(consumRecord);
        return Result.success();
    }

    @GetMapping("/getCount")
    public Result<?> findMember(@RequestParam String memberId) {

        Map map = new HashMap();

        // 余额
        Member member = memberService.getById(memberId);
        if (Objects.nonNull(member)) {
            Double balance = member.getBalance();
            map.put("count1", balance);
        } else {
            map.put("count1", 0);
        }
        // 预约总数
        LambdaQueryWrapper<MemberOrder> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(MemberOrder::getMemberId, Integer.valueOf(memberId));
        wrapper1.eq(MemberOrder::getOrderStatus, "1");
        Integer integer = memberOrderService.count(wrapper1);
        map.put("count2", integer);

        // 设备总数
        List<Equipment> equipment = equipmentService.list();
        int sum = 0;
        if (CollectionUtil.isNotEmpty(equipment)) {
            sum = equipment.stream().mapToInt(Equipment::getCount).sum();
        }
        map.put("count3", sum);

        // 订购课程
        Integer integer1 = consumRecordService.count(new LambdaQueryWrapper<ConsumRecord>().eq(ConsumRecord::getMemberId, memberId));
        map.put("count4", integer1);

        return Result.success(map);
    }


}
